package com.estun.backroundManagementService.modules.position.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.estun.backroundManagementService.aop.operationLog.Log;
import com.estun.backroundManagementService.aop.operationLog.LogConstants;
import com.estun.backroundManagementService.domain.BasePageRequest;
import com.estun.backroundManagementService.domain.BaseResponse;
import com.estun.backroundManagementService.modules.position.dto.FeedingPortParam;
import com.estun.backroundManagementService.modules.position.dto.PositionParam;
import com.estun.backroundManagementService.modules.position.model.Position;
import com.estun.backroundManagementService.modules.position.service.FeedingPortService;
import com.estun.backroundManagementService.modules.position.service.PositionService;
import com.estun.backroundManagementService.utils.QueryWrapperUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 工位表 前端控制器
 * </p>
 *
 * @author machine
 * @since 2023-02-02
 */
@RestController
@Api(tags = "PositionController", description = "工位管理")
@RequestMapping("/basic/position")
public class PositionController {

    @Autowired
    private PositionService positionService;

    @Autowired
    private FeedingPortService feedingPortService;


    @ApiOperation(value = "工位列表查询(new)")
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    @ResponseBody
    public BaseResponse page(@RequestBody BasePageRequest<Position> pageRequest) {
        Page<Position> page = new Page<>(pageRequest.getPage(), pageRequest.getSize());
        QueryWrapper<Position> queryWrapper = QueryWrapperUtil.buildQueryWrapper(pageRequest.getQuery());
        Page<Position> page1 = positionService.page(page, queryWrapper);
        return BaseResponse.success(page1);
    }


    @ApiOperation("新增工位")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    @Log(module = "工位管理 新增", type = LogConstants.INSERT, desc = "工位管理 新增")
    public BaseResponse create(@Validated @RequestBody Position positionParam) {
        boolean result = positionService.save(positionParam);
        if (!result) {
            return BaseResponse.error("新增工位失败");
        }
        return BaseResponse.success("新增工位成功");
    }

    @ApiOperation("更新工位")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    @Log(module = "工位管理 更新", type = LogConstants.UPDATE, desc = "工位管理 更新")
    public BaseResponse update(@Validated @RequestBody Position positionParam) {
        boolean result = positionService.updateById(positionParam);
        if (!result) {
            return BaseResponse.error("更新工位失败");
        }
        return BaseResponse.success("更新工位成功");
    }

    @ApiOperation("删除工位")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    @Log(module = "工位管理 删除", type = LogConstants.DELETE, desc = "工位管理 删除")
    public BaseResponse delete(@PathVariable Integer id) {
        boolean result = positionService.delete(id);
        if (!result) {
            return BaseResponse.error("删除工位失败");
        }
        return BaseResponse.success("删除工位成功");
    }

    @ApiOperation("查询所有工位")
    @GetMapping(value = "/list")
    @ResponseBody
    public BaseResponse list() {
        return BaseResponse.success(positionService.list());
    }


    @ApiOperation("查询所有工位投料口树")
    @GetMapping(value = "/listTree")
    @ResponseBody
    public BaseResponse listTree() {
        return BaseResponse.success(positionService.listTree());
    }

    @ApiOperation("绑定上料口")
    @RequestMapping(value = "/bindFeedingPort", method = RequestMethod.POST)
    @ResponseBody
    public BaseResponse bindFeedingPort(@Validated @RequestBody FeedingPortParam feedingPortParam) {
        boolean result = feedingPortService.bindFeedingPort(feedingPortParam);
        if (!result) {
            return BaseResponse.error("绑定上料口失败");
        }
        return BaseResponse.success("绑定上料口成功");
    }

    @ApiOperation("根据工位查询")
    @GetMapping(value = "/feedingPortList")
    @ResponseBody
    public BaseResponse feedingPortList(String stationId) {
        return BaseResponse.success(feedingPortService.listByStationId(stationId));
    }
}

